<?php
class FixAjaxTableEditor extends AjaxTableEditor {
function displayTableHtml()
	{
		$html = '';
		$numRows = 0;
		$this->extraOrderByInfo = empty($this->extraOrderByInfo) ? '' : ', '.$this->extraOrderByInfo;
        // Added tick marks using implod/explode in case custom order by columns have periods in them.
		$query = $this->selectClause.' '.$this->joinClause.' '.$this->whereClause.' order by '.$this->addTickMarks($this->orderByColumn).' '.$this->getAscOrDesc().' '.$this->extraOrderByInfo.' limit '.$this->start.', '.$this->displayNum;
        if($this->viewQuery)
		{
			$this->information[] = '<div id="mateViewQuery" align="left">'.nl2br($query).'</div>';
		}
		$result = $this->doQuery($query);
		if(mysql_num_rows($result) > 0)
		{
			$html .= '<div><form id="'.$this->varPrefix.'_table_form" style="margin: 0px;"><table '.$this->tableInfo.'>
			<tr class="header">';
			if(stristr($this->permissions,'M'))
			{
				$html .= '<td id="select_all_cb_cell" width="40" align="left"><input type="checkbox" id="select_all_cb" onclick="selectCbs(this,\''.$this->varPrefix.'\');" /></td>';
			}
			
			if(stristr($this->permissions,'I') && $this->iconColPosition == 'first')
			{
				$html .= strlen($this->iconTitle) > 0 ? '<td>'.$this->iconTitle.'</td>' : '<td>&nbsp;</td>';
			}
			foreach($this->tableColumns as $col => $info)
			{
				if(stristr($info['perms'],'T'))
				{
					$colHeaderInfo = isset($info['col_header_info']) ? $info['col_header_info'] : '';
					if($this->orderByColumn == $col)
					{
						list($oppAscOrDesc,$arrow) = $this->ascOrDesc == 'asc' ? array('desc','&uarr;') : array('asc','&darr;');
						$html .= '<td '.$colHeaderInfo.' ><a href="javascript: toAjaxTableEditor(\'order_by_changed\', new Array(\''.$col.'\',\''.$oppAscOrDesc.'\'));">'.$info['display_text'].'</a> '.$arrow.'</td>';
					}
					else
					{
						$html .= '<td '.$colHeaderInfo.' ><a href="javascript: toAjaxTableEditor(\'order_by_changed\', new Array(\''.$col.'\',\'asc\'));">'.$info['display_text'].'</a></td>';			
					}
				}
			}
			foreach($this->userColumns as $column)
			{
				$html .= isset($column['title']) ? '<td>'.$column['title'].'</td>' : '<td>&nbsp;</td>';
			}
			if(stristr($this->permissions,'I') && $this->iconColPosition == 'last')
			{
				$html .= strlen($this->iconTitle) > 0 ? '<td>'.$this->iconTitle.'</td>' : '<td>&nbsp;</td>';
			}
			$html .= '</tr>';
			$bgColor = $this->evenRowColor;
			while($row = mysql_fetch_assoc($result))
			{
				$numRows++;
				$bgColor = $bgColor == $this->oddRowColor ? $this->evenRowColor : $this->oddRowColor;
				$cb = '';
				$rowInfo = '';
				if(stristr($this->permissions,'M'))
				{
					$rowInfo = 'onclick="cellClicked(\''.$numRows.'\');" style="cursor: pointer;"';
					$cb = '<td><input class="rowCheckBox" type="checkbox" id="cb_'.$numRows.'" onclick="checkBoxClicked(this)" value="'.$row[$this->primaryKeyCol].'" /></td>';
				}
				
				/* pengjc
				$html .= '<tr bgcolor="'.$bgColor.'" id="row_'.$numRows.'" '.$rowInfo.' class="ajaxRow">'.$cb;
				*/
				$html .= '<tr style="background-color:'.$bgColor.';" id="row_'.$numRows.'" '.$rowInfo.' class="ajaxRow">'.$cb;
								
				if(stristr($this->permissions,'I') && $this->iconColPosition == 'first')
				{
					$html .= $this->formatIcons($row[$this->primaryKeyCol],$row);
				}				
				foreach($this->tableColumns as $col => $info)
				{
					if(stristr($info['perms'],'T'))
					{
						$value = $row[$col];
                        $tableCellInfo = isset($info['table_cell_info']) ? $info['table_cell_info'] : '';
						if(isset($info['table_fun']) && is_callable($info['table_fun']))
						{
							$value = call_user_func($info['table_fun'],$col,$value,$row);
						}
						$value = strlen(trim($value)) > 0 ? $value : '&nbsp;';
						if(isset($info['sub_str']) && strlen($value) > $info['sub_str'])
						{
							$value = substr($value,0,$info['sub_str']).'...';
						}
						if($this->searchString && $this->useHighlight && $value != '&nbsp;')
						{
							$value = $this->highlightSearchString($this->searchString,$value);
						}
						$html .= '<td '.$tableCellInfo.'>'.$value.'</td>';
					}
				}
				foreach($this->userColumns as $column)
				{
					if(isset($column['call_back_fun']) && is_callable($column['call_back_fun']))
					{
						$html .= call_user_func($column['call_back_fun'],$row);
					}
				}
				if(stristr($this->permissions,'I') && $this->iconColPosition == 'last')
				{
					$html .= $this->formatIcons($row[$this->primaryKeyCol],$row);
				}
				$html .= '</tr>';
			}
			$html .= '</table></form></div>';
		}
		else
		{
			$html = '<b>'.$this->langVars->ttlNoRecord.'</b>';
		}
		$this->retArr[] = array('layer_id' => 'tableLayer', 'where' => 'innerHTML', 'value' => $html);

		if($this->recordInfo)
		{
			if($this->numResults > 0)
			{
				$end = $this->displayNum + $this->start;
				$end = $end < $this->numResults ? $end : $this->numResults;
				$start = $this->start + 1;
				$recordHtml = '<div>'.sprintf($this->langVars->ttlDispRecs,$start,$end,number_format($this->numResults)).'</div>';
			}
			else
			{
				$recordHtml = '<div>'.$this->langVars->ttlDispNoRecs.'</div>';
			}
			$this->retArr[] = array('layer_id' => 'recordLayer', 'where' => 'innerHTML', 'value' => $recordHtml);
		}
		
		if(is_callable($this->tableScreenFun))
		{
			call_user_func($this->tableScreenFun);
		}
	}
}

class RegTableEditor extends FixAjaxTableEditor {
	
	function insertRow()
	{
		$this->valError = false;
		$this->info = get_object_vars($this->info);
		$sets = array();
		$afterAddArray = array();
		foreach($this->info as $col => $val)
		{
			$col = str_replace($this->inputIdPrefix,'',$col);
			
			if(isset($this->tableColumns[$col]['on_add_fun']) && is_callable($this->tableColumns[$col]['on_add_fun']))
			{
				$val = call_user_func($this->tableColumns[$col]['on_add_fun'],$col,$val,$this->info);
			}
			// Check Validation
			if(isset($this->tableColumns[$col]['val_fun']) && is_callable($this->tableColumns[$col]['val_fun']) && !call_user_func($this->tableColumns[$col]['val_fun'],$col,$val,$this->info))
			{
				$this->valError = true;
				$this->retArr[] = array('where' => 'javascript', 'value' => '$("'.$col.'_label_cell").setStyle("'.$this->valErrorStyles.'");');			
			}
			else if(isset($this->tableColumns[$col]['req']) && $this->tableColumns[$col]['req'])
			{
				if(strlen($val) == 0)
				{
					$this->valError = true;
					$this->retArr[] = array('where' => 'javascript', 'value' => '$("'.$col.'_label_cell").setStyle("'.$this->valErrorStyles.'");');
				}
				else if(isset($this->tableColumns[$col]['calendar']) && substr($val,0,10) == '0000-00-00')
				{
					$this->valError = true;
					$this->retArr[] = array('where' => 'javascript', 'value' => '$("'.$col.'_label_cell").setStyle("'.$this->valErrorStyles.'");');
				}
			}
			else if(!isset($this->tableColumns[$col]['hidden_add']) || !$this->tableColumns[$col]['hidden_add'])
			{
				$this->retArr[] = array('where' => 'javascript', 'value' => '$("'.$col.'_label_cell").setStyle("'.$this->noValErrorStyles.'");');
			}
			$val = $this->escapeData($val);			
			
			if(isset($this->tableColumns[$col]['mysql_add_fun']))
			{
				$sets[] = $this->addTickMarks($col)." = ".$this->tableColumns[$col]['mysql_add_fun']."('".$val."')";				
				$afterAddArray[$col] = $val;
			}
			else
			{
				$sets[] = $this->addTickMarks($col)." = '".$val."'";
				$afterAddArray[$col] = $val;
			}
		}

		if($this->valError)
		{
			$this->retArr[] = array('layer_id' => 'titleLayer', 'where' => 'innerHTML', 'value' => $this->addRowTitle.'<div style="color: #f90d0d;">Please correct the fields in red</div>');
		}
		else
		{
			$query = "insert into $this->tableName set ".implode(', ',$sets);
			$result = $this->doQuery($query);
			if($result)
			{
				if(!empty($this->afterAddFun) && is_callable($this->afterAddFun))
				{
					call_user_func($this->afterAddFun,mysql_insert_id(),$this->primaryKeyCol,$afterAddArray);
				}
			}
			// pengjc
			$this->retArr[] = array('where' => 'javascript', 'value' => "alert('You have successfully registered for the Northern California God-ordained Way Training. Thank you for giving yourself to take the trained way for a revitalized living and fruitful service to the Lord in the Body. There will be further fellowship with you regarding the details of the training as the time approaches. Meanwhile, continue to follow the Lord and seek the building with the saints in the flow of life.');");
		}
	}
	
}
?>